package com.bjpower.services.impl;
import java.math.BigDecimal;
import java.util.Date;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpower.common.constants.Sysconts;
import com.bjpower.common.enums.PublicStatus;
import com.bjpower.common.enums.SysCode;
import com.bjpower.common.exceptions.type.SysException;
import com.bjpower.domain.PayWay;
import com.bjpower.mapper.PayWayMapper;
import com.bjpower.models.request.PayWayConfigRequest;
import com.bjpower.models.request.RemovePayWayRequest;
import com.bjpower.models.request.UserPageInfoRequest;
import com.bjpower.models.vo.PayWayDicVo;
import com.bjpower.services.PayWayService;
import com.bjpower.settings.BossConfiguration;
import com.bjpower.utlis.RedisHelper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PayWayListServiceImpl implements PayWayService {

    @Resource
    private PayWayMapper payWayMapper;

    @Resource
    private RedisHelper redisHelper;
    @Override
    public IPage<PayWay> getPayWayList(UserPageInfoRequest pageInfoRequest) {
        if (StrUtil.isNotBlank(pageInfoRequest.getQueryProductCode())){
            QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("edit_time").eq("pay_product_code", pageInfoRequest.getQueryProductCode());
            IPage<PayWay> payWayIPage = payWayMapper.selectPage(
                    Page.of(pageInfoRequest.getPageNum(), pageInfoRequest.getPageSize()), wrapper);
            return payWayIPage;
        }else{
            QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("edit_time");
            IPage<PayWay> payWayIPage = payWayMapper.selectPage(
                    Page.of(pageInfoRequest.getPageNum(), pageInfoRequest.getPageSize()), wrapper);
            return payWayIPage;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletePayWay(RemovePayWayRequest removePayWay) {
        QueryWrapper<PayWay> wrapper = new QueryWrapper<PayWay>()
                .eq("way_code", removePayWay.getWayCode())
                .eq("type_code", removePayWay.getTypeCode())
                .eq("pay_product_code", removePayWay.getProductCode());
        int i = payWayMapper.delete(wrapper);
        if (i == 0) {
            throw new SysException(SysCode.DELETE_PRODUCT_FAIL);
        }
    }

    @Override
    public List<PayWayDicVo> getProductDic(UserPageInfoRequest pageInfoRequest) {
        ArrayList<PayWayDicVo>  dicVoList = new ArrayList<>();
        //计算获得开始位置以及结束位置
        int start = (pageInfoRequest.getPageNum() - 1) * pageInfoRequest.getPageSize();
        int end = start + pageInfoRequest.getPageSize() - 1;
        //从数据库中获取支付方式
        List<String> list = redisHelper.getList(Sysconts.PAY_PRODUCT_LIST, start, end);
        ObjectMapper objectMapper = new ObjectMapper();
        for (String s : list) {
            PayWayDicVo payWayJsonVo = null;
            try {
                payWayJsonVo = objectMapper.readValue(s, PayWayDicVo.class);
                PayWayDicVo payWayDicVo = new PayWayDicVo();
                payWayDicVo.setCode(payWayJsonVo.getCode());
                payWayDicVo.setName(payWayJsonVo.getName());
                dicVoList.add(payWayDicVo);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }

        return dicVoList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createPayWay(PayWayConfigRequest payWayConfigRequest) {
        //支付方式+支付方式+支付类型只能有一个
        if (payWayMapper.selectOne(new QueryWrapper<PayWay>()
                .eq("pay_product_code", payWayConfigRequest.getProductCode())
                .eq("way_code", payWayConfigRequest.getWayCode())
                .eq("type_code", payWayConfigRequest.getTypeCode())) != null){
            throw new SysException(SysCode.PAY_WAY_EXIST);
        }
        PayWay payWay = new PayWay();
        payWay.setId(IdWorker.get32UUID());
        payWay.setWayCode(payWayConfigRequest.getWayCode());
        payWay.setTypeCode(payWayConfigRequest.getTypeCode());
        payWay.setWayName(BossConfiguration.DIC_PAY_WAY_MAP.get(payWayConfigRequest.getWayCode()));
        payWay.setTypeName(BossConfiguration.DIC_PAY_TYPE_MAP.get(payWayConfigRequest.getTypeCode()));
        payWay.setPayProductCode(payWayConfigRequest.getProductCode());
        payWay.setPayRate(new BigDecimal("0"));
        payWay.setSorts(payWayConfigRequest.getSorts());
        payWay.setStatus(PublicStatus.ENABLE.name());
        payWay.setEditTime(new Date());
        payWay.setRemark("创建支付方式");
        payWayMapper.insert(payWay);

    }
}
